<?php
namespace App\library;

use Log;
/*send txt msg*/

class Msg {
	/*
	 * @return OK if success
	 * */

	public static function send_sms($to,$text){
		// 发送短信
		$send_type = config('constants.send_type');
		if($send_type==1){
			return self::send($to,$text);
		}elseif($send_type==2){
			return self::tong_send($to,$text);
		}elseif($send_type==3){
			$text = $text.' 回TD退订【极客邦】';
			return self::third_send($to,$text);
		}
	}
	public static function send($to,$text){
		$text = iconv('UTF-8', 'GBK',$text);
		$presendTime = date('Y-m-d H:i:s');
		$params = array(
			//'username'=>'69014:admin',
			//'password'=>'72995336',
			'username'=>'69428:admin',
			'password'=>'97858864',
			'from'=>'15921532458',
			'to'=>$to,
			'content'=>$text,
			'presendTime'=>$presendTime
			//  'expandPrefix'=>'113'
		);
		$url = 'http://GATEWAY.IEMS.NET.CN/GsmsHttp';
		$url = $url.'?'.http_build_query($params);
		$ret = @file_get_contents($url);

        Log::notice('SMS: '. strval($ret) .' <= '. $url);

		return self::check_sms($ret);
	}

	public static function check_sms($ret){
		if($ret === false){
			return "请求失败";
		}
		list($check, $data) = explode(":", $ret, 2);

		if($check=="OK"){
			return "OK";
        }
        elseif($check=="ERROR"){
			switch (trim($data))
			{
				case 'eUser' :
					return "用户名称有误";
					break;
				case 'eDate':
					return "发送收件格式不正确";
					break;
				case 'eIllegalPhone':
					return "发送号码错误";
					break;
				case 'ePassword':
					return "密码错误";
					break;
				case 'eStop ':
					return "用户已经停用";
					break;
				case 'eDenyDate':
					return "账户过期";
					break;
				case 'eBalance':
					return "余额不足";
					break;
				case 'eFrequent':
					return "请求频繁";
					break;
				case 'eContentLen':
					return "短信内容超长";
					break;
				case 'nContent ':
					return "短信内容为空";
					break;
				case 'eContentWrong ':
					return "短信模板拦截";
					break;
			}
		}
        return '未知错误: '. $ret;
	}


	public static function tong_send($phone,$content){
		$account = "dh26801";	   //账号
		$password = md5("@N1*uv13"); //密码
		$smsid  =  "";							//查状态报告需要带msgid,可为空，为空是查询多条状态报告
		//发送短信

		$sendSmsAddress = "http://3tong.net/http/sms/Submit";
		$message ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			."<message>"
			. "<account>"
			. $account
			. "</account><password>"
			. $password
			. "</password>"
			. "<msgid></msgid><phones>"
			. $phone
			. "</phones><content>"
			. $content
			. "</content><subcode>"
			."</subcode>"
			."<sendtime></sendtime>"
			."</message>";
		$params = array(
			'message' => $message);

		$data = http_build_query($params);
		$context = array('http' => array(
			'method' => 'POST',
			'header'  => 'Content-Type: application/x-www-form-urlencoded',
			'content' => $data,
		));
		$contents = file_get_contents($sendSmsAddress, false, stream_context_create($context));
		$res = simplexml_load_string($contents);

		return $res->result  == 0 ? "OK" : $res->desc ;
	}

	public static function third_send($to,$text){
		$loginName = 'chichuang';
		$password = substr(md5('alibaba1'),8,16);

		$smsKind = '808';
		$sendSim = $to;
		$expSmsId = "";
		$msgContext = $text;
		$msgContext = iconv('UTF-8', 'GB2312',$msgContext);
		$url = "http://sdk.zyer.cn/SmsService/SmsService.asmx/SendEx?LoginName="
			.$loginName."&Password=".$password."&SmsKind=".$smsKind."&SendSim="
			.$sendSim."&&ExpSmsId=".$expSmsId."&MsgContext=".$msgContext;
		$url = str_replace(" ", "%20", $url);
		$res =  file_get_contents($url);

		$result = simplexml_load_string($res);

		return $result->Code  == 0 ? "OK" : $result->errorDesc ;
	}

}
